package com.education.user.service;

import com.education.user.pojo.User;
import com.github.pagehelper.PageHelper;
import com.education.common.pojo.userData;
import com.education.common.utils.Token;
import com.education.user.pojo.SysUser;
import com.education.user.pojo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * 系统用户 服务层
 *
 * @author d
 * @date 2020-01-17
 */
@Service
public class ISysUserService {

    @Autowired
    private com.education.user.mapper.sysUserMapper sysUserMapper;


    //查询用户信息
    public List<SysUser> select(SysUser user) {
        List<SysUser> userList = sysUserMapper.select(user);
        return userList;
    }

    public SysUser selectUserListFeign(SysUser sysUser) {
        return sysUserMapper.selectUserListFeign(sysUser);
    }

    //查询达人币总数
    public Integer selectSumPrice(SysUser user) {
        Integer integer = sysUserMapper.selectSumPrice(user);
        return integer;
    }

    //模糊查
    public List<SysUser> findByLikePhone(SysUser user) {
        return sysUserMapper.findByLikePhone(user);
    }

    //根据主键修改用户信息
    public Integer updateById(SysUser user) {
        Example example = new Example(SysUser.class);
        example.createCriteria().andEqualTo("id", user.getId());
        return sysUserMapper.updateByExampleSelective(user, example);
    }

    //根据主键查询
    public SysUser selectByPrimaryKey(Integer userId) {
        return sysUserMapper.selectByPrimaryKey(userId);
    }

    public SysUser selectSysUserBysysPhone(String phone) {
        SysUser sysUser = new SysUser();
        sysUser.setSysPhone(phone);
        SysUser select = sysUserMapper.selectOne(sysUser);
        return select;
    }

    //查询列表
    public List<SysUser> list(Map<String, Object> map, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return sysUserMapper.listOther(map);
    }

    //查询列表
    public List<SysUser> listAll(SysUser user) {
        return sysUserMapper.select(user);
    }

    //详情
    public SysUser selectOne(String id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(Integer.parseInt(id));
        return sysUserMapper.selectOne(sysUser);
    }

    //详情
    public SysUser selectTwo(String phone) {
        SysUser sysUser = new SysUser();
        sysUser.setSysPhone(phone);
        return sysUserMapper.selectOne(sysUser);
    }

    //添加
    public int insert(SysUser user) {
        return sysUserMapper.insertSelective(user);
    }


    //修改
    public int update(SysUser user) {
        //使用自定义
        SysUser sysUser = new SysUser();
        //id
        if (user.getId() != null) {
            sysUser.setId(user.getId());
        }
        //登录名称
        if (user.getSysRelname() != null && !"".equals(user.getSysRelname())) {
            sysUser.setSysRelname(user.getSysRelname());
        }
        //年龄
        if (user.getSysAge() != null && !"".equals(user.getSysAge())) {
            sysUser.setSysAge(user.getSysAge());
        }
        //密码
        if (user.getSysPassword() != null && !"".equals(user.getSysPassword())) {
            sysUser.setSysPassword(user.getSysPassword());
        }
        //状态
        if (user.getSysState() != null && !"".equals(user.getSysState())) {
            sysUser.setSysState(user.getSysState());
        }
        //手机号
        if (user.getSysPhone() != null && !"".equals(user.getSysPhone())) {
            sysUser.setSysPhone(user.getSysPhone());
        }
        //性别
        if (user.getSysSex() != null && !"".equals(user.getSysSex())) {
            sysUser.setSysSex(user.getSysSex());
        }
        //班級
        if (user.getClassId() != null) {
            sysUser.setClassId(user.getClassId());
        }

        sysUser.setExpireDate(user.getExpireDate());

        if (user.getSchoolId() != null && !"".equals(user.getSchoolId())) {
            sysUser.setSchoolId(user.getSchoolId());
        }

        if (user.getIdcardno() != null && !"".equals(user.getIdcardno())) {
            sysUser.setIdcardno(user.getIdcardno());
        }
        return sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }

    //根据手机号进行修改密码
    public int updatePassword(SysUser user) {
        user.setSysPassword(Token.getMD5String(user.getSysPassword()));
        return sysUserMapper.updatePassword(user);
    }

    //修改
    public int updateImg(SysUser user) {
        //使用自定义
        SysUser sysUser = new SysUser();
        //id
        if (user.getId() != null) {
            sysUser.setId(user.getId());
        }
        // 头像
        if (user.getSysIco() != null && !"".equals(user.getSysIco())) {
            sysUser.setSysIco(user.getSysIco());
        }
        Integer i = sysUserMapper.updateImgForTeacherTable(sysUser);
        return sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }

    //删除
    public int deleteById(Integer id) {
        return sysUserMapper.deleteByPrimaryKey(id);
    }

    /**
     * 进行关联查询 角色名称 和 部门名称
     */
    public List<SysUserVO> selectUserList(SysUser user) {
        return sysUserMapper.selectUserList(user);
    }

    ;

    /**
     * 查询讲授人
     */
    public List<SysUser> selectUserListOther(SysUser user) {
        return sysUserMapper.selectUserListOther(user);
    }

    ;

    /**
     * 查询单个用户的详细信息
     */
    public SysUser selectOneUser(SysUser user) {
        return sysUserMapper.selectOneUser(user);
    }


    /**
     * 预约课程时查询教师
     */
    public List<SysUser> selectUserListRoom(SysUser user) {
        return sysUserMapper.selectUserListRoom(user);
    }


    public SysUser selectByDeptId(userData data) {
        return sysUserMapper.selectByDeptId(data);
    }

    public User selectUserById(Integer userId) {
        return sysUserMapper.selectUserById(userId);
    }

    public Integer updateNewPassword(User user) {
        return sysUserMapper.updateNewPassword(user);
    }
}